#include "base_controller.h"
#include "main_controller.h"

BaseController::BaseController(MainController *main_controller, const SlaveIndex slave_index)
    :main_controller_(main_controller),master_device_index_(nullptr)
{
    slave_target_index_=slave_index;
}

BaseController::~BaseController()
{

}

//void BaseController::SendUserData(const u_char *data, const size_t len)
//{
//    main_controller_->SendUserData(data, len);
//}
bool BaseController::PackFrameData(u_char *buffer, size_t &buffer_len)
{
    size_t usr_data_length=0;
    bool ret = PackUsrData(buffer+11,usr_data_length);//usr data length without ending two bytes crc

    if(!ret)
    {
        buffer_len=0;
        return false;
    }
    buffer[0]=SunlinkProtocal::kSunlinkHeader1;
    buffer[1]=SunlinkProtocal::kSunlinkHeader2;
    buffer[2]=SunlinkProtocal::kSunlinkVersion;

    TypeTransfer<u_int16_t>::Type2ByteArray(buffer+3,static_cast<u_int16_t>(usr_data_length+2+6));//pure_usr_data+len+data_type+target

    TypeTransfer<u_int16_t>::Type2ByteArray(buffer+5,static_cast<u_int16_t>(RC2Robot_Control));
    TypeTransfer<int32_t>::Type2ByteArray(buffer+7,static_cast<int32_t>(main_controller_->GetTargetRobotIndex()));

    u_int16_t crc=CalCRC16(buffer,usr_data_length+11);
    TypeTransfer<u_int16_t>::Type2ByteArray(buffer+11+usr_data_length,crc);//crc
    buffer_len=11+usr_data_length+2;                               //head+usr_data+crc

    return true;
}



main_controller::RunningStatusType BaseController::GetRunningStatus() const
{
    return  main_controller_->GetRunningStatus();
}

main_controller::SocketConnectionStatusType BaseController::GetConnectionStatus() const
{
    return main_controller_->GetConnectionStatus();
}
//char BaseController::GetExternOrder() const
//{
//    return ' ';//remote_controller_->key_;
//}

//bool BaseController::SetExternOrder(const char order)
//{
//    //remote_controller_->key_=order;
//    return true;
//}

